<?php

namespace App\Service\Coin;

use App\Constants\ErrorCode;
use App\Model\PayOrder;
use App\Service\BaseService;

/**
 * 查询订单状态
 */
class CheckOrderService extends BaseService
{
    // protected function RoVerifyList()
    // {
    //     return [
    //         'out_trade_no' => 'required|string'
    //     ];
    // }

    public function handle()
    {
        $userId = $this->getUserId();
        $outTradeId = $this->request->input('out_trade_no', '');
        $wxPrepayId = $this->request->input('wx_prepay_id', '');

        if (!$userId || (!$outTradeId && !$wxPrepayId)) {
            return $this->setError(ErrorCode::PARAMETER_ERROR, '信息参数异常~');
        }

        //订单前检测
        if ($outTradeId)  $order = PayOrder::getByOutTradeNo($outTradeId);
        elseif ($wxPrepayId) $order = PayOrder::getByWxPrepayId($wxPrepayId);

        if (!$order || $order['user_id'] != $userId) {
            return $this->setError(ErrorCode::ORDER_NOT_EXISTS, '查询不到该订单信息~');
        }

        //创建订单
        return [
            'order_id' => $order['order_id'],
            'status' => $order['status'],
            'status_desc' => PayOrder::STATUS[$order['status']],
            'pay_money' => $order['total_amount']/100,
            
        ];
    }
}
